package com.panda.pandawash.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.panda.pandawash.R;
import com.panda.pandawash.activity.MotorMonitorActivity;
import com.panda.pandawash.model.MotorDeviceInfoMessage;
import com.panda.pandawash.model.MotorSingleStatusMessage;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by gaowf on 2017/2/22.
 */

public class MonitorRunningInfoFragment extends Fragment {
    private String TAG = "MonitorInfoFragment";
    public final static int CONNECTED_FLAG = 0x10;
    public final static int UPDATE_STATUS_INFO = 0x21;
    public final static String MOTOR_RUNING = "runing";
    public final static String MOTOR_STATUS_INFO = "devstatus";
    private Handler mStatusHandler;

    private TextView motorWorkingvoltageTextView;
    private TextView motorWorkingCurrenTextView;
    private TextView motorWorkingXiangTextView;
    private TextView motorWorkingFreqTextView;

    private TextView motorSwitchtypeTextView;
    private TextView motorStartupDelayTextView;
    private TextView motorShutdownDelayTextView;
    private TextView motorShutgunDelayTextView;

    private TextView motorStartGunTimesTextView;
    private TextView motorRunedTimeTextView;

    private TextView motorEnableLeakTextView;
    private TextView motorEnableOverloadTextView;

    private TextView motorCurrentVoltageTextView;
    private TextView motorCurrentCurrenTextView;

    private TextView motorMaxContinueRunningTextView;

    private LinearLayout motorUnsupportedFeatureLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View fragmentView = inflater.inflate(R.layout.monitor_info_layout,
                container, false);

        motorWorkingvoltageTextView = (TextView) fragmentView
                .findViewById(R.id.textview_v_define);
        motorWorkingCurrenTextView = (TextView) fragmentView
                .findViewById(R.id.textview_i_define);
        motorWorkingXiangTextView = (TextView) fragmentView
                .findViewById(R.id.textview_xiangwei);
        motorWorkingFreqTextView = (TextView) fragmentView
                .findViewById(R.id.textview_freq);

        motorSwitchtypeTextView = (TextView) fragmentView
                .findViewById(R.id.textview_switch_type);
        motorStartupDelayTextView = (TextView) fragmentView
                .findViewById(R.id.textview_powergun_delay);
        motorShutdownDelayTextView = (TextView) fragmentView
                .findViewById(R.id.textview_shutdown_delay);
        motorShutgunDelayTextView = (TextView) fragmentView
                .findViewById(R.id.textview_shutgun_delay_time);

        motorRunedTimeTextView = (TextView) fragmentView
                .findViewById(R.id.textview_working_time);
        motorStartGunTimesTextView = (TextView) fragmentView
                .findViewById(R.id.textview_powergun_times);

        motorEnableLeakTextView = (TextView) fragmentView
                .findViewById(R.id.textview_enable_leak);
        motorEnableOverloadTextView = (TextView) fragmentView
                .findViewById(R.id.textview_enable_overload);

        motorCurrentCurrenTextView = (TextView) fragmentView
                .findViewById(R.id.textview_motor_current);
        motorCurrentVoltageTextView = (TextView) fragmentView
                .findViewById(R.id.textview_motor_voltage);

        motorUnsupportedFeatureLayout = (LinearLayout) fragmentView
                .findViewById(R.id.layout_unsupport_00);

        motorMaxContinueRunningTextView = (TextView)fragmentView.findViewById(R.id.textview_max_running_time);

        mStatusHandler = new Handler();
        return fragmentView;
    }

    Runnable mstatusRunnable = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            MotorMonitorActivity mMotorMonitorActivity = (MotorMonitorActivity) getActivity();
            mMotorMonitorActivity.RobotStatus();
        }
    };

    Handler mHandler = new Handler() {

        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case CONNECTED_FLAG:

                    break;
                case UPDATE_STATUS_INFO: {
                    // handle info
                    String info = msg.getData().getString(MOTOR_STATUS_INFO);

                    if (info == null) {
                        Log.i(TAG, "info is null");
                        return;
                    }
                    Log.i(TAG, info);

                    try {
                        JSONObject object = new JSONObject(info);

                        // String motor_version =
                        // object.getString(MotorDeviceInfoMessage.MOTOR_VERSION);

                        String motor_working_voltage = "220 伏";
                        String motor_working_current = object
                                .getString(MotorDeviceInfoMessage.MOTOR_WORKING_CURRENT);
                        String motor_working_xiang = object
                                .getString(MotorDeviceInfoMessage.MOTOR_XIANGWEI);

                        String motor_switch_type = object
                                .getString(MotorDeviceInfoMessage.MOTOR_START_SELECTED_WAYS);
                        String motor_startgun_delay = object
                                .getString(MotorDeviceInfoMessage.MOTOR_STARTGUN_DELAY);
                        String motor_shutdown_delay = object
                                .getString(MotorDeviceInfoMessage.MOTOR_SHUTDOWN_DELAY);
                        String motor_shutgun_delay = object
                                .getString(MotorDeviceInfoMessage.MOTOR_SHUTGUN_DELAY);

                        String motor_runed_time = object
                                .getString(MotorDeviceInfoMessage.MOTOR_RUNED_TIME);
                        String motor_startgun_times = object
                                .getString(MotorDeviceInfoMessage.MOTOR_START_TIMES);

                        String motor_enable_overload = object
                                .getString(MotorDeviceInfoMessage.MOTOR_OVERLOAD_SHIELD);
                        String motor_enable_leak = object
                                .getString(MotorDeviceInfoMessage.MOTOR_ENABLE_DETECT_LEAK);

                        String motor_current_voltage = object
                                .getString(MotorSingleStatusMessage.MOTOR_SINGLE_STATUS_VOLTAGE);
                        String motor_current_current = object
                                .getString(MotorSingleStatusMessage.MOTOR_SINGLE_STATUS_CURRENT);


                        int motor_running_time = object.getInt(MotorDeviceInfoMessage.MOTOR_MAX_RUNNING_TIME);
                        motorWorkingvoltageTextView.setText(motor_working_voltage);
                        motorWorkingCurrenTextView.setText(motor_working_current);
                        motorWorkingXiangTextView.setText(motor_working_xiang);

                        motorSwitchtypeTextView.setText(motor_switch_type);
                        motorShutdownDelayTextView.setText(motor_shutdown_delay);
                        motorStartupDelayTextView.setText(motor_startgun_delay);
                        motorShutgunDelayTextView.setText(motor_shutgun_delay);

                        motorRunedTimeTextView.setText(motor_runed_time);
                        motorStartGunTimesTextView.setText(motor_startgun_times);

                        motorEnableLeakTextView.setText(motor_enable_leak);
                        motorEnableOverloadTextView.setText(motor_enable_overload);

                        motorCurrentCurrenTextView.setText(motor_current_current);
                        motorCurrentVoltageTextView.setText(motor_current_voltage);
                        motorMaxContinueRunningTextView.setText(Integer.toString(motor_running_time));

                        if (motor_startgun_delay.equals("无")) {
                        }

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                break;
                default:
                    break;
            }
        };
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub

        switch (resultCode) {
            case CONNECTED_FLAG:
                break;
            case UPDATE_STATUS_INFO:
                Message message = new Message();
                message.what = UPDATE_STATUS_INFO;

                Bundle bundle = new Bundle();
                bundle.putString(MOTOR_STATUS_INFO,
                        data.getStringExtra(MOTOR_STATUS_INFO));
                message.setData(bundle);
                mHandler.sendMessage(message);
                break;
            default:
                break;
        }

    }
}

